import 'dart:ui';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:mahjong/util/DictionaryConst.dart';
import 'package:mahjong/util/PagingEasyRefresh.dart';
import 'package:mahjong/widget/CommonAlterDialog.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

typedef _CallBack = void Function(String roomCode);

class RecordDialog extends StatefulWidget {
  @override
  _RecordDialogState createState() => _RecordDialogState();
}

class _RecordDialogState extends State<RecordDialog> {
  TextStyle defaultWhiteTextStyle =
      TextStyle(color: Colors.white, fontSize: 12.sp);
  TextStyle defaultBlackTextStyle =
      TextStyle(color: Colors.black, fontSize: 12.sp);
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return CommonAlterDialog(
      backgroundColor: Colors.white,
      child: Container(
          margin: EdgeInsets.all(ScreenUtil().setHeight(30)),
          child: PagingEasyRefresh(
            buildItem: (item) {
              return recordItem(item);
            },
          )),
    );
  }

/*  BackdropFilter(
            filter: ImageFilter.blur(sigmaX: 0.3, sigmaY: 0.3),
            child:) */
  Widget recordItem(dynamic data) {
    String roomCode = data["roomCode"] ?? "未知";
    String startDate = data["startDate"] ?? "未知";
    String endDate = data["endDate"] ?? "未知";
    String roomType = data["roomType"] ?? "未知";
    return Column(
      children: [
        Container(
          color: Colors.black.withOpacity(0.1),
          padding: EdgeInsets.all(5.w),
          margin: EdgeInsets.only(bottom: 5.w),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Expanded(
                  flex: 9,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          textContainer("房间类型:", roomType),
                          textContainer("房间号:", roomCode),
                          textContainer("游戏时间:", startDate + "--" + endDate)
                        ],
                      ),
                      SizedBox(height: 5.h),
                      Container(
                          child: SingleChildScrollView(
                              scrollDirection: Axis.horizontal,
                              child: Row(
                                children: List.castFrom(data["users"])
                                    .map((item) => userWidget(
                                        item["username"],
                                        item["portraitUrl"],
                                        item["fraction"].toString()))
                                    .toList(),
                              )))
                    ],
                  )),
              Expanded(child: Icon(Icons.arrow_forward_ios))
            ],
          ),
        ),
      ],
    );
  }

  Container userWidget(String name, String headImg, String fraction) {
    return Container(
        width: 100.h,
        padding: EdgeInsets.all(1.h),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(1.h),
          /*   border: Border.all(color: Colors.blue.shade500) */
        ),
        child:
            Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
          ClipRRect(
              borderRadius: BorderRadius.circular(3.w),
              child: Container(
                height: 30.h,
                width: 30.h,
                child: headImg == null
                    ? Image.asset(
                        "images/mahjong/user/header/common_header.png",
                        fit: BoxFit.cover,
                      )
                    : CachedNetworkImage(
                        fit: BoxFit.cover,
                        height: 20.h,
                        width: 20.h,
                        placeholder: (context, url) =>
                            CircularProgressIndicator(),
                        errorWidget: (context, url, error) => Icon(Icons.error),
                        imageUrl: DictionaryConst.filePath + headImg,
                      ),
              )),
          Expanded(
              child: Container(
                  height: 30.h,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Text(fraction ?? "未知",
                          style: TextStyle(
                              color: Colors.red,
                              fontSize: 12.sp,
                              fontWeight: FontWeight.w600)),
                      Text(name ?? "未知",
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: defaultBlackTextStyle),
                    ],
                  )))
        ]));
  }

  Row textContainer(String title, String context) {
    return Row(
      children: [
        Text(
          title,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: defaultBlackTextStyle,
        ),
        SizedBox(
          width: 5.w,
        ),
        Container(
          height: 18.h,
          alignment: Alignment.center,
          child: Text(
            context,
            textAlign: TextAlign.center,
            style: defaultWhiteTextStyle,
          ),
          padding: EdgeInsets.symmetric(horizontal: 5.w),
          decoration: BoxDecoration(
              color: Colors.orange, borderRadius: BorderRadius.circular(5.w)),
        ),
        SizedBox(
          width: 5.w,
        ),
      ],
    );
  }
}
/* class DefalutPading{
  DefalutPading._();
  const static EdgeInsets.
} */
